package com.Lixiaoqian.CardPlay.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.adapter.NavAdapter;
import com.Lixiaoqian.CardPlay.base.MyApp;
import com.Lixiaoqian.CardPlay.bean.NavItemBean;
import com.Lixiaoqian.CardPlay.common.customview.RoundAngleImageView;
import com.Lixiaoqian.CardPlay.utils.Constant;
import com.Lixiaoqian.CardPlay.utils.LoadImageView;
import com.Lixiaoqian.CardPlay.utils.ToastUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.R.attr.bitmap;


public class MainActivity extends AutoLayoutActivity
        implements View.OnClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.imageView)
    RoundAngleImageView userImage;
    @BindView(R.id.tv_user_name)
    TextView userName;
    @BindView(R.id.nav_listView)
    ListView navListView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;
    private ShareAction shareAction;
    private MainActivity mactivity = this;
    private NavAdapter navAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        findView();
        initToolBar();
        initNavItem();
        initFloatButton();
        initEvent();
        initShare();
    }

    private void initNavItem() {
        ArrayList<NavItemBean> navItemBeens = new ArrayList<>();
        int[] images = {R.mipmap.nav_find, R.mipmap.nav_setting, R.mipmap.nav_help, android.R.drawable.ic_menu_share, android.R.drawable.ic_menu_send,};
        String[] info = {"发 现", "设 置", "帮 助", "分 享", "反 馈"};
        for (int i = 0; i < images.length; i++) {
            NavItemBean navItemBean = new NavItemBean(images[i], info[i]);
            navItemBeens.add(navItemBean);
        }
        navAdapter = new NavAdapter(navItemBeens, this);
        navListView.setAdapter(navAdapter);
    }

    private void initShare() {
        //分享回调
        shareAction = new ShareAction(this).setDisplayList(SHARE_MEDIA.WEIXIN,
                SHARE_MEDIA.SINA);
        shareAction.setCallback(new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                ToastUtils.showShort(MainActivity.this, "启动中....");
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                ToastUtils.showShort(MainActivity.this, "分享失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
            }
        });
    }

    private void initEvent() {
        userImage.setOnClickListener(this);
        navListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        CommonWebActivity.launch(mactivity, "发现", Constant.FIND_URL);
                        break;
                    case 1:
                        SetActivity.launch(mactivity);
                        break;
                    case 2:
                        CommonWebActivity.launch(mactivity, "帮助", Constant.HELP_URL_RECI);
                        break;
                    case 3:
                        shareContext();
                        break;
                    case 4:
                        SuggestionActivity.launch(mactivity);
                        break;
                }
            }
        });
    }

    /**
     * 初始化FloatButton
     */
    private void initFloatButton() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    }

    private void findView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    }

    /**
     * 初始化toolbar
     */
    private void initToolBar() {
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }


    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

/*    */

    /**
     * item点击事件
     *
     * @return
     *//*
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.

        int id = item.getItemId();
        if (id == R.id.nav_find) {
            CommonWebActivity.launch(this, "发现", Constant.FIND_URL);

        } else if (id == R.id.nav_set) {
            SetActivity.launch(this);
        } else if (id == R.id.nav_help) {
            CommonWebActivity.launch(this, "帮助", Constant.HELP_URL_RECI);
        } else if (id == R.id.nav_share) {
            shareContext();
        } else if (id == R.id.nav_send) {
            SuggestionActivity.launch(this);
        }
        // drawer.closeDrawer(GravityCompat.START);
        return true;
    }*/
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView:
                launchActivity();
                break;
        }
    }

    public void launchActivity() {
        if (MyApp.isLogin()) {
            //跳转到用户信息界面
        } else {
            // LoginActivity.launch(this);
            PersonActivity.launch(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == Constant.REQUESTCODE && resultCode == Constant.RESULTCODE) {
                /*LoadImageView.LoadImgUser(this,MyApp.user.getAvatarimagePath(),userImage);
                userName.setText(MyApp.user.getUsername());*/
                userName.setText("进水阀咖啡");
        }
    }

    /**
     * 分享逻辑,内容设置
     */
    public void shareContext() {
        ShareBoardConfig config = new ShareBoardConfig();
        //String[] split = news_image_path.split(":");
        config.setShareboardPostion(ShareBoardConfig.SHAREBOARD_POSITION_BOTTOM);
        config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_CIRCULAR);
        config.setCancelButtonVisibility(true);
        config.setTitleText("选择分享平台");
        UMWeb umWeb = SetShareContent(Constant.APP_DOWN, "分享卡播App");
        shareAction.withMedia(umWeb);
        shareAction.open(config);
    }

    public UMWeb SetShareContent(String url, String title) {
        UMImage image = new UMImage(this, Constant.SHARE_IMAGE_SINA);//网络图片
        UMWeb umWeb = new UMWeb(url);
        umWeb.setThumb(image);
        umWeb.setTitle(title);//标题
        umWeb.setDescription("梦幻土楼的分享内容");//描述
        return umWeb;
    }
}
